package com.dongdongshop.service.impl;

import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    @Transactional
    public void insertInfo(TbGoods goods, TbGoodsDesc goodsDesc, TbItem tbItem) {
        goods.setAuditStatus("0");
        tbGoodsMapper.insertSelective(goods);
        Long id = goods.getId();
        goodsDesc.setGoodsId(id);
        tbGoodsDescMapper.insertSelective(goodsDesc);
        tbItem.setCreateTime(new Date());
        tbItem.setUpdateTime(new Date());
        tbItem.setGoodsId(id);
        tbItemMapper.insertSelective(tbItem);
    }

    @Override
    public Page<TbGoods> getInfo(TbGoods tbGoods, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoods> list = tbGoodsMapper.selectByName(tbGoods);
        PageInfo<TbGoods> pageInfo = new PageInfo<>(list);
        Page<TbGoods> page = new Page<>(pageInfo.getPageNum(),pageInfo.getSize(),pageInfo.getPages(),pageInfo.getList());
        return page;
    }

    @Override
    public void updateInfo(Long[] id, String auditStatus) {
        if (auditStatus.equals("1")) {
            rocketMQTemplate.convertAndSend("dongdongshop-topic",new Gson().toJson(id));
        }
        tbGoodsMapper.updateById(id,auditStatus);
    }

    @Override
    @Transactional
    public void deleteInfo(Long[] id) {
        tbGoodsMapper.deleteById(id);
        tbGoodsDescMapper.deleteById(id);
        tbItemMapper.deleteById(id);
    }

    @Override
    public TbGoods findGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoodsDesc findGoodsDescById(Long goodsId) {
        return tbGoodsDescMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public List<TbItem> selectByGoodsId(Long goodsId) {
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);//还得属于某一个SPU
        criteria.andStockCountGreaterThan(0);//库存还要>0
        criteria.andStatusEqualTo("1");
        return tbItemMapper.selectByExample(example);
    }
}
